﻿#include "Self_Define_Functions.h"
#include <time.h>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <iomanip>
#include <string>
#include<cstring>
#include <stdio.h>
#include <math.h>
#include <malloc.h>
#include <cstdlib>
#define PI 3.1415926535897932384626433832795029
double *OShift, *M, *y, *z, *x_bound;
int ini_flag = 0, n_flag, func_flag, *SS;
using namespace std;
#define RandInt(n)	(rand()%n) 

int main(int argc, char *argv[])
{
	//////////////////////////global processing/////////////////////////////////////

	int funToRun[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30 };  //function set

	int funNum = 30; //total function num
	double optima[30] = { 100,200,300,400,500,600,700,800,900,1000,1100,1200,1300,1400,1500,1600,1700,1800,1900,2000,2100,2200,2300,2400,2500,2600,2700,2800,2900,3000 };

	/////////////////////////////////////////////////////////////////////////////////////
	
	srand(time(NULL));
	clock_t  start_T0, end_T0,start_T1, end_T1, start_T2, end_T2;
	int i, j;
	double x = 0.55;
	start_T0 = clock();
	for (i = 0; i < 1000000; i++)
	{
		x = x + x; x = x / 2; x = x * x, x = sqrt(x); x = log(x); x = exp(x); x = x / (x + 2);
	}
	end_T0 = clock();
	
	double derta1, derta2;
	double p;
	double pp1 = 0.15;
	double dd1 = 0.1;
	int NP = 150;
	NewType *sorted_data = new NewType[NP];

	double p_J = 0.2;//The parameter of JADE top best individual
	int top_gbest_num = ceil(NP * p_J);
	int A_num = 0;//The number of individuals in Archive
	double Erfa = 0.5;
	double p_num;//Elite group
	double *c_strategy_pro = new double[8]; //Probability of each mutation strategy being selected according to convergence
	double *d_strategy_pro = new double[8];//Probability of each mutation strategy being selected according to diversity
	double *select_mutation_pro_c = new double[8];//Save the probability for roulette wheel selection 
	double *select_mutation_pro_d = new double[8];//Save the probability for roulette wheel selection 
	double sum_con;
	double sum_dis;
	int *select_mutation = new int[NP];//record the  selected mutation strategy
	double global_fitness_old;
	double mid_CR;
	double mid_F;
	double *CR=new double[NP];
	double *F = new double[NP];
	double MIN = -100;//Low bound
	double MAX = 100;//Up bound
	double *CR_record = new double[NP];
	double *F_record = new double[NP];
	double *global_best = new double[dim];
	double global_fitness;
	int *sorted_index = new int[NP];
	int *individual_rank = new int[NP];
	double **population = new double*[NP];
	double **A = new double*[NP];//Archive
	for (i = 0; i < NP; i++)
	{
		CR[i] = F[i] = (i + 1.0) / NP;
		population[i] = new double[dim];
		A[i] = new double[dim];
	}
	double *results = new double[NP];// the fitness results for the whole population
	double *trail_vector_results = new double[NP];
	int FV = 0;
	int iter = 0;
	int function_index;
	int run_index;
	int parent1, parent2, parent3, parent4, parent5;
	double *mutation_vector = new double[dim];
	double **trail_vector = new double*[NP];
	for (i = 0; i < NP; i++)
		trail_vector[i] = new double[dim];
	int FV_T1 = 0, FVmax_T1 = 200000;
	for (function_index = 17; function_index < 18; function_index++)
	{
		//cout << "Function " << funToRun[function_index] << " Begined!" << endl;
		//write the results into files
		char fun1[10];
		char fun2[10];
		char fun3[10];
		snprintf(fun1, 10, "%d", funToRun[function_index]);
		snprintf(fun3, 10, "%d", dim);
		snprintf(fun2, 10, "%d", NP);
		string filename_fitness = "./N" + string(fun2) + "_dim" + string(fun3) + "/Fitness_result_for_function_" + string(fun1) + ".txt";
		ofstream out_fitness(filename_fitness.c_str());

		if (!out_fitness)
		{
			cerr << "Can not open the file " << filename_fitness << endl;
			exit(1);
		}
		
		start_T1 = clock();
		while (FV_T1 < FVmax_T1)
		{
			for (i = 0; i < NP; ++i)
			{
				for (j = 0; j < dim; j++)
				{
					population[i][j] = generate_random_real(MIN, MAX);
				}

				cec14_test_func(population[i], &results[i], dim, 1, funToRun[function_index]);
				results[i] = results[i] - optima[function_index];
			}
			FV_T1 += NP;
			

		}
		end_T1 = clock();
		for (run_index = 0; run_index < timesOfRun; run_index++)
		{
			FV = 0;
			A_num = 0;
			//Initialization
			for (i = 0; i < NP; ++i)
			{
				for (j = 0; j < dim; j++)
				{
					population[i][j] = generate_random_real(MIN,MAX);
				}
				cec14_test_func(population[i], &results[i], dim, 1, funToRun[function_index]);
				results[i] = results[i] - optima[function_index];
			}
			FV = NP;
			Sort_Population_Fitness(sorted_index, individual_rank, results, NP, sorted_data);
			global_fitness = results[sorted_index[0]];
			start_T2 = clock();

			while (FV < MAX_FV)
			{
				//Calculate the parameter for mutation select
				derta1 = 0.1 + dd1 * pow((1.0*FV / MAX_FV), Erfa);
				derta2 = 1 - derta1;
				p = 0.05 + pp1 * pow((1.0*FV / MAX_FV), Erfa);
				p_num = p * NP;
				sum_con = 0;
				sum_dis = 0;
				for (i = 0; i < 8; i++)
				{
					//calculate the weight for each muation strategy 
					c_strategy_pro[i] = 1 / (sqrt(2 * PI)*derta1 * 8)*exp(pow(7 - i, 2) / (2 * derta1*derta1 * 8 * 8)*(-1));
					d_strategy_pro[i] = 1 / (sqrt(2 * PI)*derta2 * 8)*exp(pow(i, 2) / (2 * derta2*derta2 * 8 * 8)*(-1));
					sum_con += c_strategy_pro[i];
					sum_dis += d_strategy_pro[i];
				}
				//calculate the probability for each muation strategy 
				for (i = 0; i < 8; i++)
				{
					c_strategy_pro[i] = c_strategy_pro[i] / sum_con;
					d_strategy_pro[i] = d_strategy_pro[i] / sum_dis;
				}
				select_mutation_pro_c[0] = c_strategy_pro[0];
				select_mutation_pro_d[0] = d_strategy_pro[0];
				//calculate the probability for roulette wheel selection 
				for (i = 1; i < 8; i++)
				{
					select_mutation_pro_c[i] = select_mutation_pro_c[i - 1] + c_strategy_pro[i];
					select_mutation_pro_d[i] = select_mutation_pro_d[i - 1] + d_strategy_pro[i];
				}

				Sort_Population_Fitness(sorted_index, individual_rank, results, NP, sorted_data);
				global_fitness_old = global_fitness;
				global_fitness = results[sorted_index[0]];
				memcpy(global_best, population[sorted_index[0]], sizeof(double)*dim);
				for (i = 0; i < NP; i++)
				{
					mid_CR = generate_gaussian_random(CR[individual_rank[i]],0.1);
					mid_F = generate_gaussian_random(F[individual_rank[i]],0.1);
					while (mid_CR <= 0 || mid_CR >= 1)
					{
						mid_CR = generate_gaussian_random(CR[individual_rank[i]], 0.1);
					}
					CR_record[i] = mid_CR;
					while (mid_F <= 0)
					{
						mid_F = generate_gaussian_random(F[individual_rank[i]], 0.1);
					}
					if (mid_F >= 1)
						mid_F = 1;
					F_record[i] = mid_F;
					
				}

				for (i = 0; i < NP; ++i)
				{
					if (individual_rank[i] < p_num)
					{
						select_mutation[i] = Roulette_Selection(select_mutation_pro_c, 8) + 1;
					}
					else
					{
						select_mutation[i] = Roulette_Selection(select_mutation_pro_d, 8) + 1;
					}

					switch (select_mutation[i])
					{

					case 1:
						parent1 = generate_random_integer(0,NP-1);
						while (parent1 == i)
						{
							parent1 = generate_random_integer(0, NP - 1);
						}

						parent2 = generate_random_integer(0, NP - 1);
						while (parent2 == parent1 || parent2 == i)
						{
							parent2 = generate_random_integer(0, NP - 1);
						}

						parent3 = generate_random_integer(0, NP - 1);
						while (parent3 == i || parent3 == parent1 || parent3 == parent2)
						{
							parent3 = generate_random_integer(0, NP - 1);
						}
						parent4 = generate_random_integer(0, NP - 1);
						while (parent4 == i || parent4 == parent1 || parent4 == parent2 || parent4 == parent3)
						{
							parent4 = generate_random_integer(0, NP - 1);
						}
						parent5 = generate_random_integer(0, NP - 1);
						while (parent5 == i || parent5 == parent1 || parent5 == parent2 || parent5 == parent3 || parent5 == parent4)
						{
							parent5 = generate_random_integer(0, NP - 1);
						}
						Mutation_rand_2(population[i], mutation_vector, population[parent1], population[parent2], population[parent3], population[parent4], population[parent5], MIN, MAX, F_record[parent1], dim);
						break;

					case 2:
						parent1 = generate_random_integer(0, NP - 1);
						while (parent1 == i)
						{
							parent1 = generate_random_integer(0, NP - 1);
						}

						parent2 = generate_random_integer(0, NP - 1);
						while (parent2 == parent1 || parent2 == i)
						{
							parent2 = generate_random_integer(0, NP - 1);
						}
						parent3 = generate_random_integer(0, NP - 1);
						while (parent3 == parent2 || parent3 == parent1 || parent3 == i)
						{
							parent3 = generate_random_integer(0, NP - 1);
						}
						Mutation_rand_1(population[i], mutation_vector, population[parent1], population[parent2], population[parent3], MIN, MAX, F_record[parent1], dim);
						break;
					case 3:
						parent1 = generate_random_integer(0, NP - 1);
						while (parent1 == i)
						{
							parent1 = generate_random_integer(0, NP - 1);
						}

						parent2 = generate_random_integer(0, NP - 1);
						while (parent2 == parent1 || parent2 == i)
						{
							parent2 = generate_random_integer(0, NP - 1);
						}

						parent3 = generate_random_integer(0, NP - 1);
						while (parent3 == i || parent3 == parent1 || parent3 == parent2)
						{
							parent3 = generate_random_integer(0, NP - 1);
						}
						parent4 = generate_random_integer(0, NP - 1);
						while (parent4 == i || parent4 == parent1 || parent4 == parent2 || parent4 == parent3)
						{
							parent4 = generate_random_integer(0, NP - 1);
						}
						parent5 = generate_random_integer(0, NP - 1);
						while (parent5 == i || parent5 == parent1 || parent5 == parent2 || parent5 == parent3 || parent5 == parent4)
						{
							parent5 = generate_random_integer(0, NP - 1);
						}
						Mutation_current_to_rand_2(population[i], mutation_vector, population[i], population[parent1], population[parent2], population[parent3], population[parent4], population[parent5], MIN, MAX, F_record[i], dim);
						break;

					case 4:
						parent1 = generate_random_integer(0, NP - 1);
						while (parent1 == i)
						{
							parent1 = generate_random_integer(0, NP - 1);
						}

						parent2 = generate_random_integer(0, NP - 1);
						while (parent2 == parent1 || parent2 == i)
						{
							parent2 = generate_random_integer(0, NP - 1);
						}
						parent3 = generate_random_integer(0, NP - 1);
						while (parent3 == i || parent3 == parent1 || parent3 == parent2)
						{
							parent3 = generate_random_integer(0, NP - 1);
						}
						Mutation_current_to_rand_1(population[i], mutation_vector, population[i], population[parent1], population[parent2], population[parent3], MIN, MAX, F_record[i], dim);
						break;

					case 5:
						parent5 = generate_random_integer(0, top_gbest_num);
						while (sorted_index[parent5] == i)
						{
							parent5 = generate_random_integer(0, top_gbest_num);
						}
						parent1 = generate_random_integer(0, NP - 1);
						while (parent1 == i || parent1 == sorted_index[parent5])
						{
							parent1 = generate_random_integer(0, NP - 1);
						}
						parent2 = generate_random_integer(0, NP - 1);
						while (parent2 == parent1 || parent2 == i || parent2 == sorted_index[parent5])
						{
							parent2 = generate_random_integer(0, NP - 1);
						}
						parent3 = generate_random_integer(0, NP - 1);
						while (parent3 == i || parent3 == parent1 || parent3 == parent2 || parent3 == sorted_index[parent5])
						{
							parent3 = generate_random_integer(0, NP - 1);
						}
						parent4 = generate_random_integer(0, NP+A_num-1);
						while (parent4 == i || parent4 == parent1 || parent4 == parent2 || parent4 == parent3 || parent4 == sorted_index[parent5])
						{
							parent4 = generate_random_integer(0, NP + A_num - 1);
						}
						if (parent4 < NP)
						{
							//size of Archive <= NP
							Mutation_cur_pbest_2(population[i], mutation_vector, population[i], population[sorted_index[parent5]], population[parent1], population[parent2], population[parent3], population[parent4], MIN, MAX, F_record[i], dim);
						}
						else
						{
							//size of Archive > NP
							Mutation_cur_pbest_2(population[i], mutation_vector, population[i], population[sorted_index[parent5]], population[parent1], population[parent2], population[parent3], A[parent4 - NP], MIN, MAX, F_record[i], dim);
						}
						break;

					case 6:
						parent3 = generate_random_integer(0, top_gbest_num);
						while (sorted_index[parent3] == i)
						{
							parent3 = generate_random_integer(0, top_gbest_num);
						}
						parent1 = generate_random_integer(0, NP - 1);
						while (parent1 == i || parent1 == sorted_index[parent3])
						{
							parent1 = generate_random_integer(0, NP - 1);
						}
						parent2 = generate_random_integer(0, NP + A_num - 1);
						while (parent2 == i || parent2 == parent1 || parent2 == sorted_index[parent3])
						{
							parent2 = generate_random_integer(0, NP + A_num - 1);
						}
						if (parent2 < NP)
						{
							Mutation_cur_pbest_1(population[i], mutation_vector, population[i], population[sorted_index[parent3]], population[parent1], population[parent2], MIN, MAX, F_record[i], dim);
						}
						else
						{
							Mutation_cur_pbest_1(population[i], mutation_vector, population[i], population[sorted_index[parent3]], population[parent1], A[parent2 - NP], MIN, MAX, F_record[i], dim);
						}
						break;

					case 7:
						parent1 = generate_random_integer(0, NP - 1);
						while (parent1 == i || parent1 == sorted_index[0])
						{
							parent1 = generate_random_integer(0, NP - 1);
						}
						parent2 = generate_random_integer(0, NP - 1);
						while (parent2 == parent1 || parent2 == i || parent2 == sorted_index[0])
						{
							parent2 = generate_random_integer(0, NP - 1);
						}
						parent3 = generate_random_integer(0, NP - 1);
						while (parent3 == i || parent3 == parent1 || parent3 == parent2 || parent3 == sorted_index[0])
						{
							parent3 = generate_random_integer(0, NP - 1);
						}
						parent4 = generate_random_integer(0, NP - 1);
						while (parent4 == i || parent4 == parent1 || parent4 == parent2 || parent4 == parent3 || parent4 == sorted_index[0])
						{
							parent4 = generate_random_integer(0, NP - 1);
						}
						Mutation_current_to_best_2(population[i], mutation_vector, population[i], global_best, population[parent1], population[parent2], population[parent3], population[parent4], MIN, MAX, F_record[i], dim);
						break;

					case 8:
						parent1 = generate_random_integer(0, NP - 1);
						while (parent1 == i || parent1 == sorted_index[0])
						{
							parent1 = generate_random_integer(0, NP - 1);
						}
						parent2 = generate_random_integer(0, NP - 1);
						while (parent2 == i || parent2 == parent1 || parent2 == sorted_index[0])
						{
							parent2 = generate_random_integer(0, NP - 1);
						}
						Mutation_current_to_best_1(population[i], mutation_vector, population[i], global_best, population[parent1], population[parent2], MIN, MAX, F_record[i], dim);
						break;
					}
					Crossover(trail_vector[i], population[i], mutation_vector, CR_record[i], dim);
					cec14_test_func(trail_vector[i], &trail_vector_results[i], dim, 1, funToRun[function_index]);
					trail_vector_results[i] = trail_vector_results[i] - optima[function_index];
					
					if (trail_vector_results[i] < results[i])
					{
						if (A_num < NP)//|A|<NP
						{
							memcpy(A[A_num], population[i], sizeof(double)*dim);
							A_num++;
						}
						else//|A|==NP
						{
							memcpy(A[generate_random_integer(0, NP - 1)], population[i], sizeof(double)*dim);
						}
						results[i] = trail_vector_results[i];
						memcpy(population[i], trail_vector[i], sizeof(double)*dim);
						if (results[i] < global_fitness)
						{

							global_fitness = results[i];
							memcpy(global_best, population[i], sizeof(double)*dim);
						}
					}
					
				}
				FV += NP;
			}
			end_T2 = clock();
			out_fitness << double(end_T2 - start_T2) / CLOCKS_PER_SEC << endl;
			//out_fitness << global_fitness << endl;

		}
		out_fitness << double(end_T0 - start_T0) / CLOCKS_PER_SEC << endl;
		out_fitness << double(end_T1 - start_T1) / CLOCKS_PER_SEC << endl;
		out_fitness.close();

		//cout << "Function " << funToRun[function_index] << " Finished!" << endl;

	}


	//release the resouces
	for (i = 0; i < NP; ++i)
	{
		delete[]population[i];
		delete[]trail_vector[i];
		delete[]A[i];
	}
	delete[]select_mutation_pro_c;
	delete[]select_mutation_pro_d;
	delete[]c_strategy_pro;
	delete[]d_strategy_pro;

	delete[]population;
	delete[]A;
	delete[]trail_vector;
	delete[]individual_rank;

	delete[]results;
	delete[]trail_vector_results;
	delete[]mutation_vector;

	delete[]CR_record;
	delete[]F_record;
	delete[]global_best;
	delete[]sorted_index;

	return 0;
}
bool Compare_NewType(NewType data1, NewType data2)
{
	return data1.data < data2.data;
}
double generate_random_real(double min, double max) 
{
	return min + (max - min) * ((double)rand() / RAND_MAX);
}

double generate_gaussian_random(double mean, double stddev)
{
	static double z0, z1;
	static int generate;
	generate = !generate;

	if (!generate)
		return z1 * stddev + mean;

	double u1, u2;
	do {
		u1 = rand() * (1.0 / RAND_MAX);
		u2 = rand() * (1.0 / RAND_MAX);
	} while (u1 <= DBL_EPSILON);

	double mag = sqrt(-2.0 * log(u1));
	z0 = mag * cos(2.0 * PI * u2);
	z1 = mag * sin(2.0 * PI * u2);

	return z0 * stddev + mean;
}
int generate_random_integer(int x, int y) 
{
	return x + rand() % (y - x + 1);
}
void Sort_Population_Fitness(int *sorted_index, int *individual_rank, double *population_results, int population_size, NewType *sorted_data)
{

	int i, j;

	for (i = 0; i < population_size; ++i)
	{
		sorted_data[i].data = population_results[i];
		sorted_data[i].id = i;
	}
	sort(sorted_data, sorted_data + population_size, Compare_NewType);

	for (i = 0; i < population_size; ++i)
	{
		sorted_index[i] = sorted_data[i].id;
		individual_rank[sorted_data[i].id] = i;
	}
}


void Mutation_current_to_rand_1(double *population, double *mutation_vector, double *parent1, double *parent2, double *parent3, double *parent4, double Lbound, double Ubound, double F, int dim)
{
	int i;

	for (i = 0; i < dim; ++i)
	{
		mutation_vector[i] = parent1[i] + F * (parent2[i] - parent1[i]) + F * (parent3[i] - parent4[i]);

		if (mutation_vector[i] < Lbound)
			mutation_vector[i] = (Lbound + population[i]) / 2;
		if (mutation_vector[i] > Ubound)
			mutation_vector[i] = (Ubound + population[i]) / 2;
	}
}

void Mutation_rand_1(double *population, double *mutation_vector, double *parent1, double *parent2, double *parent3, double Lbound, double Ubound, double F, int dim)
{
	int i;

	for (i = 0; i < dim; ++i)
	{
		mutation_vector[i] = parent1[i] + F * (parent2[i] - parent3[i]);

		if (mutation_vector[i] < Lbound)
			mutation_vector[i] = (Lbound + population[i]) / 2;
		if (mutation_vector[i] > Ubound)
			mutation_vector[i] = (Ubound + population[i]) / 2;

	}
}

void Mutation_current_to_best_1(double *population, double *mutation_vector, double *parent1, double *parent2, double *parent3, double *parent4, double Lbound, double Ubound, double F, int dim)
{
	int i;

	for (i = 0; i < dim; ++i)
	{
		mutation_vector[i] = parent1[i] + F * (parent2[i] - parent1[i]) + F * (parent3[i] - parent4[i]);

		if (mutation_vector[i] < Lbound)
			mutation_vector[i] = (Lbound + population[i]) / 2;
		if (mutation_vector[i] > Ubound)
			mutation_vector[i] = (Ubound + population[i]) / 2;

	}
}
void Mutation_cur_pbest_1(double *population, double *mutation_vector, double *parent0, double *parent1, double *parent2, double *parent3, double Lbound, double Ubound, double F, int dim)
{
	int i;

	for (i = 0; i < dim; ++i)
	{
		mutation_vector[i] = parent0[i] + F * (parent1[i] - parent0[i]) + F * (parent2[i] - parent3[i]);

		if (mutation_vector[i] < Lbound)
			mutation_vector[i] = (Lbound + population[i]) / 2;
		if (mutation_vector[i] > Ubound)
			mutation_vector[i] = (Ubound + population[i]) / 2;

	}
}
void Mutation_cur_pbest_2(double *population, double *mutation_vector, double *parent0, double *parent1, double *parent2, double *parent3, double *parent4, double *parent5, double Lbound, double Ubound, double F, int dim)
{
	int i;

	for (i = 0; i < dim; ++i)
	{
		mutation_vector[i] = parent0[i] + F * (parent1[i] - parent0[i]) + F * (parent3[i] - parent2[i]) + F * (parent4[i] - parent5[i]);

		if (mutation_vector[i] < Lbound)
			mutation_vector[i] = (Lbound + population[i]) / 2;
		if (mutation_vector[i] > Ubound)
			mutation_vector[i] = (Ubound + population[i]) / 2;
	}
}
void Mutation_current_to_best_2(double *population, double *mutation_vector, double *parent1, double *parent2, double *parent3, double *parent4, double *parent5, double *parent6, double Lbound, double Ubound, double F, int dim)
{
	int i;

	for (i = 0; i < dim; ++i)
	{
		mutation_vector[i] = parent1[i] + F * (parent2[i] - parent1[i]) + F * (parent3[i] - parent4[i]) + F * (parent5[i] - parent6[i]);

		if (mutation_vector[i] < Lbound)
			mutation_vector[i] = (Lbound + population[i]) / 2;
		if (mutation_vector[i] > Ubound)
			mutation_vector[i] = (Ubound + population[i]) / 2;

	}
}
void Mutation_current_to_rand_2(double *population, double *mutation_vector, double *parent1, double *parent2, double *parent3, double *parent4, double *parent5, double *parent6, double Lbound, double Ubound, double F, int dim)
{
	int i;

	for (i = 0; i < dim; ++i)
	{
		mutation_vector[i] = parent1[i] + F * (parent2[i] - parent1[i]) + F * (parent3[i] - parent4[i]) + F * (parent5[i] - parent6[i]);
		if (mutation_vector[i] < Lbound)
			mutation_vector[i] = (Lbound + population[i]) / 2;
		if (mutation_vector[i] > Ubound)
			mutation_vector[i] = (Ubound + population[i]) / 2;

	}
}
void Mutation_rand_2(double *population, double *mutation_vector, double *parent1, double *parent2, double *parent3, double *parent4, double *parent5, double Lbound, double Ubound, double F, int dim)
{
	int i;

	for (i = 0; i < dim; ++i)
	{
		mutation_vector[i] = parent1[i] + F * (parent2[i] - parent3[i]) + F * (parent4[i] - parent5[i]);

		if (mutation_vector[i] < Lbound)
			mutation_vector[i] = (Lbound + population[i]) / 2;
		if (mutation_vector[i] > Ubound)
			mutation_vector[i] = (Ubound + population[i]) / 2;
	}
}
void Crossover(double *trial_vector, double *individual, double *mutation_vector, double CR, int dim)
{
	int i;



	int dim_rand = generate_random_integer(0, dim - 1);

	for (i = 0; i < dim; ++i)
	{
		if (i == dim_rand || generate_random_real(0,1) <= CR)
			trial_vector[i] = mutation_vector[i];
		else
			trial_vector[i] = individual[i];
	}

}

int Roulette_Selection(double *probability, int population_size)
{
	int i, selected_index;

	
	double random_pr = generate_random_real(0, 1);

	selected_index = -1;

	for (i = 0; i < population_size; ++i)
	{
		if (random_pr <= probability[i])
		{
			selected_index = i;
			break;
		}
	}

	return selected_index;

}


